using System;
using System.Threading.Tasks;
using Admin.NET.Application.Entity.NoticeConfigs;
using Admin.NET.Core;
using Furion.DependencyInjection;
using Furion.EventBus;
using Microsoft.Extensions.DependencyInjection;
using Newtonsoft.Json;

namespace Admin.NET.Application.EventBus;

/// <summary>
/// 钉钉发布
/// </summary>
public class DingtalkEventSubscribe : IEventSubscriber, ISingleton, IPrivateDependency
{
	private readonly IServiceProvider _serviceProvider;

	public DingtalkEventSubscribe(IServiceProvider serviceProvider)
	{
		_serviceProvider = serviceProvider;
	}

	/// <summary>
	/// 发送消息到钉钉webhook
	/// </summary>
	/// <param name="context"></param>
	/// <returns></returns>
	[EventSubscribe("11:SendNoticeToManager")]
	public async Task SendNoticeToManager(EventHandlerExecutingContext context)
	{
		using IServiceScope scope = _serviceProvider.CreateScope();
		scope.ServiceProvider.GetRequiredService<SqlSugarRepository<EmailConfig>>();
		scope.ServiceProvider.GetRequiredService<SqlSugarRepository<EmailRecord>>();
		EventPayload payload = (EventPayload)context.Source.Payload;
		JsonConvert.DeserializeObject<ManagerNoticePayload>(payload.Data);
	}
}
